package com.echo.yixiaowu.main

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import coil3.ImageLoader
import coil3.compose.AsyncImage
import coil3.compose.AsyncImagePainter
import coil3.request.ImageRequest
import coil3.request.crossfade
import com.echo.yixiaowu.data.model.WaterfallItem

@Composable
fun WaterfallItemCard(
    item: WaterfallItem,
    modifier: Modifier = Modifier,
    imageLoader: ImageLoader,
) {
    val context = LocalContext.current
    val imageRequest = ImageRequest.Builder(context).data(item.url).crossfade(true)
        .placeholderMemoryCacheKey("image-${item.id}").memoryCacheKey("image-${item.id}").build()

    Card(
        modifier = modifier,
        colors = CardDefaults.cardColors(containerColor = Color.White),
        shape = RoundedCornerShape(8.dp)
    ) {
        Column {
            AsyncImage(
                model = imageRequest,
                imageLoader = imageLoader,
                contentDescription = item.title,
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .aspectRatio(1f),
                onState = { state ->
                    when (state) {
                        is AsyncImagePainter.State.Loading -> {
                            // 图片正在加载
                        }

                        is AsyncImagePainter.State.Success -> {
                            // 图片加载成功
                            Log.i("ImageLoad", "load success" + state.result)
                        }

                        is AsyncImagePainter.State.Error -> {
                            // 图片加载失败
                            Log.e("ImageLoad", "load error" + state.result.throwable)
                        }

                        else -> {
                        }
                    }
                },
            )
            // 标题和描述
            Column(
                modifier = Modifier.padding(12.dp)
            ) {
                Text(
                    text = item.title,
                    style = MaterialTheme.typography.titleMedium,
                    fontWeight = FontWeight.Bold
                )
                Spacer(modifier = Modifier.height(4.dp))
                Text(
                    text = item.description,
                    style = MaterialTheme.typography.bodySmall,
                    maxLines = 3,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
        }
    }
}